import logger from '@common/logger';

// 微信小程序支付相关方法
const wx = window.wx;
/**
 * 执行跳转微信小程序支付
 * @param {Object} param 支付的订单详情
 * @param {String} orderNumber 订单号
 * @param {String} backUrl 回调地址
 * @param {Boolean} isCodeMode 是否是取餐码模式
 */
export const invokeWechatMiniPay = (param, orderNumber, backUrl, isCodeMode=false) => {
    const payParam = {
        timeStamp: param.timeStamp,
        nonceStr: param.nonceStr,
        package: param.package,
        signType: param.signType,
        paySign: param.paySign,
    };
    const _payParam = encodeURIComponent(JSON.stringify(payParam));
    const path = `/pages/pay/index?payParam=${_payParam}&orderNumber=${orderNumber}&backUrl=${backUrl}${isCodeMode ? '&isCodeMode=' + isCodeMode : ''}`;
    logger.info('调转的链接为：', path);
    wx.miniProgram.navigateTo({
        url: path,
    });
};
export const invokeWechatMiniShopPay = (param, orderNumber, backUrl, isCodeMode=false) => {
    const payParam = {
        timeStamp: param.timeStamp,
        nonceStr: param.nonce,
        package: param.package,
        signType: param.sign_type,
        paySign: param.pay_sign,
    };
    const _payParam = encodeURIComponent(JSON.stringify(payParam));
    const path = `/pages/pay/index?payParam=${_payParam}&orderNumber=${orderNumber}&type=shop&backUrl=${backUrl}${isCodeMode ? '&isCodeMode=' + isCodeMode : ''}`;
    logger.info('调转的链接为：', path);
    wx.miniProgram.navigateTo({
        url: path,
    });
};

/**
 * 执行小程序扫码
 * @param {String} backUrl 回调函数
 */
export const invokeWechatMiniScan = (backUrl) => {
    const path = `/pages/scan/scan?backUrl=${backUrl}`;
    logger.info('跳转到微信小程序扫码：', backUrl);
    wx.miniProgram.navigateTo({
        url: path,
    });
};

export const invokeGotoMiniOrderDetailPage = ({ orderNumber }) => {
    const backUrl = encodeURIComponent(`${window.location.origin}/sole/order/detail/${orderNumber}`);
    const path = `/pages/orderdetail/orderdetail?orderNumber=${orderNumber}`;
    logger.info('跳转到微信小程序订单详情：', backUrl);
    wx.miniProgram.reLaunch({
        url: path,
    });
};

export const invokeGotoMiniShopStatus = (orderNumber='') => {
    const path = `/pages/shopOrderStatus/shopOrderStatus?orderNumber=${orderNumber}&type=shop`;
    wx.miniProgram.reLaunch({
        url: path,
    });
};

export default invokeWechatMiniPay;

